package com.samshow.vo;

import com.samshow.constant.Constant;
import com.samshow.entity.UserEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/11/23.
 */
public class SampleUserDetail implements UserDetails {
    protected static final GrantedAuthority DEFAULT_USER_ROLE = new SimpleGrantedAuthority(Constant.ROLE_PREFIX + Privilege.USER.name());

    protected List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    private String username;

    private String password;

    public  SampleUserDetail(UserEntity user, final List<Privilege> privileges){
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.grantedAuthorities.add(DEFAULT_USER_ROLE);
        for (Privilege privilege :privileges) {
            this.grantedAuthorities.add(new SimpleGrantedAuthority(Constant.ROLE_PREFIX + privilege.name()));
        }
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
